package com.kgc.warehouse.bussness.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.kgc.warehouse.bussness.entity.*;
import com.kgc.warehouse.bussness.mapper.*;
import com.kgc.warehouse.bussness.service.ProviderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;

/**
 * @author main
 * @description 针对表【bus_provider】的数据库操作Service实现
 * @createDate 2023-02-04 08:48:32
 */
@Service
@Transactional
public class ProviderServiceImpl extends ServiceImpl<ProviderMapper, Provider> implements ProviderService {

    @Resource
    private GoodsMapper goodsMapper;
    @Resource
    private InportMapper inportMapper;
    @Resource
    private OutportMapper outportMapper;
    @Resource
    private SalesMapper salesMapper;
    @Resource
    private SalesbackMapper salesbackMapper;

    @Resource
    private ProviderMapper providerMapper;

    @Override
    public void deleteProviderById(Integer id) {
        //根据供应商id查询出商品id
        LambdaQueryWrapper<Goods> goodsLambdaQueryWrapper = new LambdaQueryWrapper<>();
        goodsLambdaQueryWrapper.eq(Goods::getProviderid, id);
        List<Goods> goods = goodsMapper.selectList(goodsLambdaQueryWrapper);
        for (Goods good : goods) {
            Integer goodId = good.getId();
            //根据商品id删除商品销售信息
            LambdaQueryWrapper<Sales> salesLambdaQueryWrapper = new LambdaQueryWrapper<>();
            salesLambdaQueryWrapper.eq(Sales::getGoodsid,goodId);
            salesMapper.delete(salesLambdaQueryWrapper);
            //根据商品id删除商品销售退货信息
            LambdaQueryWrapper<Salesback> salesbackLambdaQueryWrapper = new LambdaQueryWrapper<>();
            salesbackLambdaQueryWrapper.eq(Salesback::getGoodsid,goodId);
            salesbackMapper.delete(salesbackLambdaQueryWrapper);
        }

        //根据供应商id删除商品进货信息
        LambdaQueryWrapper<Inport> inportLambdaQueryWrapper = new LambdaQueryWrapper<>();
        inportLambdaQueryWrapper.eq(Inport::getProviderid,id);
        inportMapper.delete(inportLambdaQueryWrapper);

        //根据供应商id删除商品退货信息
        LambdaQueryWrapper<Outport> outportLambdaQueryWrapper = new LambdaQueryWrapper<>();
        outportLambdaQueryWrapper.eq(Outport::getProviderid,id);
        outportMapper.delete(outportLambdaQueryWrapper);

        //删除商品
        LambdaQueryWrapper<Goods> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Goods::getProviderid,id);
        goodsMapper.delete(queryWrapper);
        //删除供应商
        providerMapper.deleteById(id);
    }
}




